﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EditBranch : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (isLogin())
        {
            if (LoginedStaff.tblRole.Role != "Admin")
                Response.Redirect("~/Admin/Index.aspx");
            if (!IsPostBack)
            {
                MailDistributionSystemDBDataContext db = new MailDistributionSystemDBDataContext();
                try
                {
                    if (Request.QueryString["id"] != null)
                    {
                        int id = Convert.ToInt16(Request.QueryString["id"]);
                        tblBranch branch = db.tblBranches.Where(c => c.ID == id).SingleOrDefault();
                        branchName.Text = branch.Name;
                        branchAddress.Text = branch.Address;
                        countryList.SelectedValue = branch.CountryID.ToString();
                    }
                    else
                    {
                        Response.Redirect("~/Admin/Branch/ViewBranch.aspx");
                    }
                }
                catch { Response.Redirect("~/Admin/Branch/ViewBranch.aspx"); }
            }
        }
        else
        {
            Response.Redirect("~/Admin/Stafflogin.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        branchLabel.Visible = false;
        addressLabel.Visible = false;

        if (!string.IsNullOrEmpty(branchName.Text) && !string.IsNullOrEmpty(branchAddress.Text))
        {
            MailDistributionSystemDBDataContext db = new MailDistributionSystemDBDataContext();
            int id = Convert.ToInt16(Request.QueryString["id"]);
            tblBranch branch = db.tblBranches.Where(c => c.ID == id).SingleOrDefault();
            tblBranch tmp = db.tblBranches.SingleOrDefault(c => c.Name == branchName.Text.Trim());
            if (tmp != null && tmp.ID != id)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Branch Already Exist!')", true);
            }
            else
            {
                branch.Name = branchName.Text.Trim();
                branch.Address = branchAddress.Text.Trim();
                branch.CountryID = Convert.ToInt16(countryList.SelectedValue);
                db.SubmitChanges();
                Response.Redirect("~/Admin/Branch/ViewBranch.aspx");
            }

        }
        else
        {
            if (string.IsNullOrEmpty(branchName.Text))
            {
                branchLabel.Visible = true;
            }

            if (string.IsNullOrEmpty(branchAddress.Text))
            {
                addressLabel.Visible = true;
            }
        }
    }
    protected void branchName_TextChanged(object sender, EventArgs e)
    {

    }
}